package bestTeam.WalkieDoggie.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.*;

import com.google.gson.Gson;

import bestTeam.WalkieDoggie.client.WalkieDoggie;
import bestTeam.WalkieDoggie.client.walkieStatus;
import bestTeam.WalkieDoggie.client.Serializables.UserProfileData;
import bestTeam.WalkieDoggie.client.Serializables.UserRequest;

public class GetTodoServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {

		String id = req.getParameter("id");

		RequestServiceImpl requestService = new RequestServiceImpl();
		HashMap<UserRequest,UserProfileData> requests = requestService.getRequestsAndOwners(id);
		ArrayList<RequestForAndroid> res = new ArrayList<RequestForAndroid>();

		if(requests!=null){


			for (Map.Entry<UserRequest, UserProfileData> entry : requests.entrySet()) {

				UserRequest currRequest = entry.getKey(); 
				if(isInMyTodo(currRequest,id)){

					UserProfileData currRequestCreater = entry.getValue();

					RequestForAndroid requestForAndroid = new RequestForAndroid();
					requestForAndroid.setCreatedBy_email(currRequestCreater.getEmail());
					requestForAndroid.setCreatedBy_id(currRequest.getCreatedBy());
					requestForAndroid.setCreatedBy_name(currRequest.getUserName());
					requestForAndroid.setDate(currRequest.getDate());
					requestForAndroid.setHouseNumber(currRequestCreater.getHouseNumber());
					requestForAndroid.setRequestId(currRequest.getId());
					requestForAndroid.setStreet(currRequestCreater.getStreet());
					requestForAndroid.setTime(currRequest.getTimeFrame());
					requestForAndroid.setDogName(currRequestCreater.getDogName());
					res.add(requestForAndroid);

				}

			}

		}

		String json = new Gson().toJson(res);
		resp.getWriter().println(json);
	}

	private boolean isInMyTodo(UserRequest request, String id) {

		
		if(id.equals(request.getApprovedWalkerID()) && request.getStatus()==walkieStatus.APPROVED)
			return true;

		return false;
	}
	
}